package com.googlecode.pathmachine.nn;

/**
 *Name: Utils
 *
 *Author: John Pendlebury ported to Java from code written by Matt Buckland
 *
 *Description: Some utility methods
 *
 *You can reuse this code provided you include these comments.
 *I'd also appreciate you letting me know via email just because I'm
 *interested.
 *
 *email: john.pendlebury2@mail.dcu.ie
 */
import java.text.DecimalFormat;
import java.util.Random;

public class Utils {

    public static double roundTwoDecimals(double d) {
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        return Double.valueOf(twoDForm.format(d));
    }
    //	Generates a random double value between -1 and +1

    public static double generateRandomClamped() {
        Random random = new Random();
        return (random.nextDouble() * 2.0) - 1.0;
    }

    //	Generates a random double value between 0 and 1
    public static double generateRandom() {
        Random random = new Random();
        return random.nextDouble();
    }

    public static double clamp(double arg, double min, double max) {
        double ret = arg;

        if (ret < min) {
            ret = min;
        }

        if (ret > max) {
            ret = max;
        }

        return ret;
    }

    static int generateRandom(int x, int y) {
        Random random = new Random();
        int rand = random.nextInt();
        return Math.abs(rand % (y - x + 1) + x);
    }
}
